﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using HalconDotNet;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageTaskModule
{
    public partial class FormImageTaskItem : DevExpress.XtraEditors.XtraForm
    {
        public static List<ImageToolTypeInfo> imageToolTypeInfos;
        ImageTask imageTask;
        public FormImageTaskItem()
        {
            InitializeComponent();
        }
        public FormImageTaskItem(ImageTask imageTaskIn)
        {
            InitializeComponent();
            imageTask= imageTaskIn;
            labelControlTaskName.Text = imageTaskIn.Name;
            if (imageToolTypeInfos == null)
            {
                dynamicLoadTool();
            }
        }
        public static void dynamicLoadTool()
        {
            imageToolTypeInfos = new List<ImageToolTypeInfo>();
            try
            {

                DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + @"\ImageTools\");
                FileInfo[] fileInfos = dirInfo.GetFiles();
                foreach (FileInfo fileInfo in fileInfos)
                {
                    string strExtension = fileInfo.Extension.ToUpper();
                    if (strExtension != ".DLL")
                    {
                        continue;
                    }
                    Assembly assembly = Assembly.LoadFile(fileInfo.FullName);

                    if (assembly == null)
                    {
                        continue;
                    }
                    Type[] types = assembly.GetTypes();
                    foreach (Type type in types)
                    {
                        MethodInfo myMethodInfo = type.GetMethod("ToolTypeRegister");
                        if (myMethodInfo != null)
                        {
                            ImageToolTypeInfo imageToolTypeInfo= myMethodInfo.Invoke(null, new object[] {}) as ImageToolTypeInfo;
                            if (imageToolTypeInfo != null)
                                imageToolTypeInfos.Add(imageToolTypeInfo);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return;
        }

        private void FormImageTaskItem_Load(object sender, EventArgs e)
        {
            gridControlToolType.DataSource = imageToolTypeInfos;
            gridControlImageTaskItems.DataSource=imageTask.listTools;

            imageTask.LoadRegeditFile(smartDisplayControl1);
            textEdit1.Text=FrameManager.FrameManager.ProductSetting.保存图片路径;
        }

        private void buttonEditImageTools_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                AddNewTool();
            }
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
            {
                DeleteTool();
            }
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.SpinUp)
            {
                MoveUpTool();
            }
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.SpinDown)
            {
                MoveDnTool();
            }
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Undo)
            {
                ModifyTool();
            }
        }
        private void AddNewTool()
        {
            if (buttonEditImageTools.Text == "")
            {
                buttonEditImageTools.Text = "Name Can't Empty";
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (buttonEditImageTools.Text.IndexOf("Can't Empty") > -1)
            {
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (buttonEditImageTools.Text.IndexOf("Already Eixt") > -1)
            {
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (imageTask.dicTools.Keys.Contains(buttonEditImageTools.Text))
            {
                buttonEditImageTools.Text = buttonEditImageTools.Text + " Already Eixt";
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            GridView gridView = gridControlToolType.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                object obj = imageToolTypeInfos[iIndex].toolType.Assembly.CreateInstance(imageToolTypeInfos[iIndex].toolType.FullName);
                if (obj is ImageDefine.ImageToolBase)
                {
                    ImageDefine.ImageToolBase newItem = (ImageDefine.ImageToolBase)obj;
                    newItem.strName = buttonEditImageTools.Text;
                    newItem.ToolTypeName = imageToolTypeInfos[iIndex].ToolName;
                    imageTask.Add(newItem);
                    newItem.CreateActioner(imageTask);
                    gridControlImageTaskItems.RefreshDataSource();
                }
            }
            else
            {
                MessageBox.Show("没有选择工具");
            }
        }
        private void DeleteTool()
        {
            
            GridView gridView = gridControlImageTaskItems.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                imageTask.Remove(iIndex);
                gridControlImageTaskItems.RefreshDataSource();
            }
            else
            {
                MessageBox.Show("没有选择工具");
            }
        }
        private void MoveUpTool()
        {

            GridView gridView = gridControlImageTaskItems.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            imageTask.ShiftUp(iIndex);
            gridControlImageTaskItems.RefreshDataSource();

        }
        private void MoveDnTool()
        {

            GridView gridView = gridControlImageTaskItems.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            imageTask.ShiftDn(iIndex);
            gridControlImageTaskItems.RefreshDataSource();
        }
        private void ModifyTool()
        {

            if (buttonEditImageTools.Text == "")
            {
                buttonEditImageTools.Text = "Name Can't Empty";
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (buttonEditImageTools.Text.IndexOf("Can't Empty") > -1)
            {
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (buttonEditImageTools.Text.IndexOf("Already Eixt") > -1)
            {
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            if (imageTask.dicTools.Keys.Contains(buttonEditImageTools.Text))
            {
                buttonEditImageTools.Text = buttonEditImageTools.Text + " Already Eixt";
                buttonEditImageTools.SelectAll();
                buttonEditImageTools.Focus();
                return;
            }
            GridView gridView = gridControlImageTaskItems.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                imageTask.Modify(iIndex, buttonEditImageTools.Text);
                gridControlImageTaskItems.RefreshDataSource();
            }
            else
            {
                MessageBox.Show("没有选择工具");
            }
        }

        private void simpleButtonActionStart_Click(object sender, EventArgs e)
        {
            imageTask.Run(smartDisplayControl1);
        }

        private void simpleButton注册图像_Click(object sender, EventArgs e)
        {
            imageTask.WriteRegeditFile(smartDisplayControl1);
        }

        private void simpleButtonEditForm_Click(object sender, EventArgs e)
        {
            if (flyoutPanel1.Visible == false)
                flyoutPanel1.ShowPopup(true);
            else
                flyoutPanel1.HidePopup(true);
        }

        private void gridControlImageTaskItems_DoubleClick(object sender, EventArgs e)
        {
            GridView gridView = gridControlImageTaskItems.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                Form frm=imageTask.listTools[iIndex].ShowSettingFrm();
                frm.TopLevel = false;
                frm.Size = this.Size;
                this.Controls.Add(frm);
                frm.Show();
                frm.BringToFront();
                flyoutPanel1.HidePopup(true);
                frm.FormClosed += Frm_FormClosed;
            }
        }

        private void Frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            flyoutPanel1.ShowPopup(true);
        }
        bool bAlreadyGetImage = false;
        int iFileImageIndex = 0;
        List<string> listImageFileNames;
        private void simpleButton运行历史图片_Click(object sender, EventArgs e)
        {
            if (listImageFileNames == null)
            {
                listImageFileNames = new List<string>();
                string[] fileNames = Directory.GetFiles(textEdit1.Text);
                foreach (string fileName in fileNames)
                {
                    listImageFileNames.Add(fileName);
                }
                iFileImageIndex = 0;
            }
            int iTempIndex = iFileImageIndex;
            if (listImageFileNames.Count < 1)
                return;
            iFileImageIndex = iFileImageIndex + 1;
            if (iFileImageIndex == listImageFileNames.Count)
            {
                iFileImageIndex = 0;
            }
            
            HalconDotNet.HImage image = new HImage(listImageFileNames[iTempIndex]);
            smartDisplayControl1.ShowImage(image);
            imageTask.RunFileImage(smartDisplayControl1, image);

        }
    }
}